<?php 
namespace Home\Model;
use Think\Model;
use Lrxin ;

class ArticleModel extends Model {

	public $__data = null; //当前数据
	public $__id;   //当前 id



	public function _initialize()
    {
        //parent::_initialize();
        
        
    }

    public function __construct(){
    	parent::__construct();
    }
    //初始化 当前文章
    public function  init($id){
        $id = intval($id);

    	$rs = $this->find($id);
        if($rs){
            $this->__id = $id;
            $this->__data = $rs; 
            return $rs;   
        }
        return false;   
    }

    //浏览量+1
    public function __click($id = null ){
        if(!$id){$id = $this->__id;}
        $this->where(" id = '{$id}'")->setInc('click',1);
        return $this;
    }
    //上一篇
    public function __prev($id = null, $flag = false){
        if(!$id){$id = $this->__id;}
        $rs = $this->order(' id desc ')->where("id < '{$id}' ")->limit(0,1)->find();
        if($flag){
            return $rs;
        }
        if(!$rs){
            return '没有了...';
        }
        $url = $this->__arturl($rs['id']);
        return "<a href=\"{$url}\">{$rs['title']}</a>";
    }


    //下一篇
    public function __next($id = null, $flag = false){
        if(!$id){$id = $this->__id;}
        $rs = $this->order(' id asc ')->where("id > '{$id}' ")->limit(0,1)->find();
        if($flag){
            return $rs;
        }
        if(!$rs){
            return '没有了...';
        }
        $url = $this->__arturl($rs['id']);
        return "<a href=\"{$url}\">{$rs['title']}</a>";
        
    }

    //文章url
    public function __arturl($id = null){
         if(!$id){$id = $this->__id;}
         return arturl($id);

    }


    //读取文章列表
    public function getlist($arr=null){
         //默认配置
        $c = array(
                'tid' =>null,
                'limit' =>10,
        );

        if(!is_array($arr)){
            $arr = array(
                'tid' => $arr ,
                );

        }
        $c = array_merge($c,$arr);
        $c['tid'] = empty($c['tid'])? $this->__data['tid']: $c['tid'];


        $map= array();
        if(!is_array($c['tid'])){ $c['tid'] = (string)$c['tid']; }
        $map['tid'] = array('in', $c['tid']);


        $rs = $this->where($map)->order('sorttime desc ')->limit( $c['limit'] )->select();

        return $rs; 

    }

    //修改默认排序
    public function order($s=null){
        if(!$s){
            $s = 'sort asc , sorttime desc , id desc ' ;
        }
        return parent::order($s);

    }

    





}
